%%%-------------------------------------------------------------------
%%% @author Viktor Green <viktor@student.gu.se>
%%% @copyright (C) 2011, Viktor Green
%%% @doc
%%% Tests for the module torrent_file.erl
%%% @end
%%% Created : 12 Dec 2011 by Viktor Green <viktor@student.gu.se>
%%%-------------------------------------------------------------------
-module(torrent_file_tests).

-include_lib("eunit/include/eunit.hrl").

torrent_file_test_() ->
    [
     fun parse/0,
     fun get_value/0
    ].

parse() ->
    {ok, Dir} = file:get_cwd(),
    Parent = filename:dirname(Dir),
    Path = filename:join(Parent, priv),
    TestFile = filename:join(Path, "Test.torrent"),
    BadTestFile = filename:join(Path, "Test.zip"),
    NonExistantTestFile = filename:join(Path, "NotHere.file"),
    {Result1, _Value1} = torrent_file:parse(TestFile),
    ?assert(Result1 == ok),
    {Result2, _Value2} = torrent_file:parse(BadTestFile),
    ?assert(Result2 == error),
    {Result3, _Value3} = torrent_file:parse(NonExistantTestFile),
    ?assert(Result3 == error).

get_value() ->
    {ok, Dir} = file:get_cwd(),
    Parent = filename:dirname(Dir),
    TestFile = filename:join([Parent, priv, "Test.torrent"]),
    Announce = "http://peertracker.local/announce",
    {ok, Data} = torrent_file:parse(TestFile),
    {Type1, Value1} = torrent_file:get_value("announce", Data),
    ?assert(string == Type1),
    ?assert(Announce == Value1),
    {Type2, Value2} = torrent_file:get_value("info", Data),
    ?assert(dictionary == Type2),
    ?assertNot(dictionary == Value2),
    {Type3, Value3} = torrent_file:get_value("not_existing", Data),
    ?assert(error == Type3),
    ?assert(not_found == Value3).
